package com.ilop.sthome.ui.activity.detail.socket;

import android.content.Intent;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.ui.adapter.detail.LinkageAdapter;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityLinkageListBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-04-06.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：插座联动列表
 */
public class LinkageListActivity extends BaseBindActivity<ActivityLinkageListBinding> {

    private String mRecommendId;
    private String mDeviceName;
    private int mDeviceId;

    private LinkageAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_linkage_list;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mRecommendId = getIntent().getStringExtra(CommonId.KEY_RECOMMEND_ID);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID, -1);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new LinkageAdapter(mContext);
        mDBind.linkageList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        LiveDataBus.get().with(CommonId.LINKAGE_ON_CLICK, Integer.class).observe(this, this::skipToEdit);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivLinkageBack.setOnClickListener(view -> finish());
        mDBind.ivAddLinkage.setOnClickListener(view -> skipToEdit(-1));
        mDBind.linkageList.setOnEmptyListener(()-> skipToEdit(-1));
        mDBind.linkageList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                refreshList();
                mDBind.linkageList.complete();
            }

            @Override
            public void onLoadMore() {
                mDBind.linkageList.complete();
                mDBind.linkageList.onNoMore();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if ("28".equals(mRecommendId)){
            mDBind.linkageTitle.setText(getString(R.string.linkage_alarm));
        }
        refreshList();
    }

    private void skipToEdit(int mid){
        Intent intent = new Intent(mContext, SocketLinkageActivity.class);
        intent.putExtra(CommonId.KEY_RECOMMEND_ID, mRecommendId);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
        intent.putExtra(CommonId.KEY_DEVICE_ID, mDeviceId);
        intent.putExtra(CommonId.KEY_AUTOMATION, mid);
        startActivity(intent);
    }

    private void refreshList(){
        List<AutomationBean> mList = AutomationDaoUtil.getInstance().findAutomationByOutType(mRecommendId, mDeviceName, mDeviceId);
        if (mList.size()>0){
            mAdapter.setList(mList);
            mDBind.ivAddLinkage.setVisibility(View.VISIBLE);
        }else {
            mAdapter.setList(null);
            mDBind.linkageList.setEmptyView(mContext.getString(R.string.add_auto), R.drawable.device_empty_add);
            mDBind.ivAddLinkage.setVisibility(View.GONE);
        }
    }

}
